using UnityEngine;

[ExecuteInEditMode]
public class ControllerBulletBallSpawner2 : ControllerGeneric
{
	public GameObject bulletBall;

	public float bulletSpeed = 1f;

	public float bulletRange = 10f;

	public float delay;

	public float interval = 1f;

	public float intervalVariation;

	public GameObject spawner;

	public GameObject receiver;

	private Vector3 bulletStart;

	private Vector3 bulletEnd;

	public GameObject startPortal;

	public GameObject endPortal;

	public override void OnLevelInPlace(Transform levelTransform)
	{
		base.OnLevelInPlace(levelTransform);
		if (levelTransform == base.transform.root)
		{
			PrepareForGame();
		}
	}

	public void PrepareForGame()
	{
		startPortal.transform.parent = base.transform;
		endPortal.transform.parent = base.transform;
		BulletBallSpawner2 bulletBallSpawner = base.gameObject.AddComponent<BulletBallSpawner2>();
		bulletBallSpawner.bulletBall = bulletBall;
		bulletBallSpawner.bulletSpeed = bulletSpeed;
		bulletBallSpawner.bulletRange = bulletRange;
		bulletBallSpawner.delay = delay;
		bulletBallSpawner.interval = interval;
		bulletBallSpawner.intervalVariation = intervalVariation;
		bulletBallSpawner.bulletStartLocal = spawner.transform.localPosition;
		bulletBallSpawner.bulletEndLocal = receiver.transform.localPosition;
		remoteControl.controller = null;
		if (Application.isPlaying)
		{
			Object.Destroy(spawner);
			Object.Destroy(receiver);
			Object.Destroy(this);
		}
		else
		{
			Object.DestroyImmediate(spawner);
			Object.DestroyImmediate(receiver);
			Object.DestroyImmediate(this);
		}
	}

	private void Update()
	{
		if (Application.isEditor && !Application.isPlaying)
		{
			SetupRemoteControl();
			if (IsRemoteControlActive())
			{
				base.transform.position = remoteControl.transform.position;
				base.transform.rotation = remoteControl.transform.rotation;
				bulletRange = remoteControl.transform.localScale.z;
			}
			if (remoteControl != null)
			{
				remoteControl.transform.localScale = new Vector3(1f, 1f, bulletRange);
				remoteControl.scale = new Vector3(1f, 1f, bulletRange);
			}
			Setup();
		}
	}

	private void Setup()
	{
		AssureParenting();
		spawner.transform.localPosition = new Vector3(0f, 0f, (0f - bulletRange) / 2f + 0.5f);
		receiver.transform.localPosition = new Vector3(0f, 0f, bulletRange / 2f - 0.5f);
	}

	public void SetupRemoteControl()
	{
		if (remoteControl == null)
		{
			InitRemoteControl();
			remoteControl.gameObject.name = remoteControl.gameObject.name + base.name;
			remoteControl.gameObject.transform.position = base.transform.position;
			remoteControl.gameObject.transform.rotation = base.transform.rotation;
			remoteControl.gameObject.transform.localScale = new Vector3(1f, 1f, bulletRange);
			remoteControl.gameObject.AddComponent<PresetsBulletBallSpawner2>();
		}
	}

	public override void CopyFromRemoteControl(RemoteControl rc)
	{
		base.CopyFromRemoteControl(rc);
		CopyParentingFromRemoteControl(rc);
		ControllerBulletBallSpawner2 component = rc.controller.GetComponent<ControllerBulletBallSpawner2>();
		bulletSpeed = component.bulletSpeed;
		delay = component.delay;
		interval = component.interval;
		intervalVariation = component.intervalVariation;
		PresetsBulletBallSpawner2 component2 = remoteControl.gameObject.GetComponent<PresetsBulletBallSpawner2>();
		component2.bulletSpeed = component.bulletSpeed;
		component2.delay = component.delay;
		component2.interval = component.interval;
		component2.intervalVariation = component.intervalVariation;
	}
}
